//% color="#48C0A3"
namespace Encoder {
    //% block="read switch 8:[PIN8] 4:[PIN4] 2:[PIN2] 1:[PIN1]" blockType="reporter"
    //% PIN8.shadow="dropdown" PIN8.options="PIN_8"
    //% PIN4.shadow="dropdown" PIN4.options="PIN_4"
    //% PIN2.shadow="dropdown" PIN2.options="PIN_2"
    //% PIN1.shadow="dropdown" PIN1.options="PIN_1"
    export function readSwitch(parameter: any, block: any) {
        let pin8 = parameter.PIN8.code;
        let pin4 = parameter.PIN4.code;
        let pin2 = parameter.PIN2.code;
        let pin1 = parameter.PIN1.code;
        if(Generator.board === 'esp32'){
            Generator.addImport("from mpython import *");
            Generator.addCode(`MPythonPin(${pin8},PinMode.IN).read_digital()*8+MPythonPin(${pin4},PinMode.IN).read_digital()*4+MPythonPin(${pin2},PinMode.IN).read_digital()*2+MPythonPin(${pin1},PinMode.IN).read_digital()`);
        }
        if(Generator.board === 'microbit'){
            Generator.addCode(`pin${pin8}.read_digital()*8+pin${pin4}.read_digital()*4+pin${pin2}.read_digital()*2+pin${pin1}.read_digital()`);
        }
        if(Generator.board === 'maixduino'){
            Generator.addImport("from board import board_info");
            Generator.addImport("from pin import Pin");
            Generator.addCode(`Pin.digital_read(board_info.PIN${pin8})*8+Pin.digital_read(board_info.PIN${pin4})*4+Pin.digital_read(board_info.PIN${pin2})*2+Pin.digital_read(board_info.PIN${pin1})`);
        }
    }

   //% block="switch value[VALUE]" blockType="reporter"
   //% VALUE.shadow="dropdown" VALUE.options="VALUE"
   export function switchValue(parameter: any, block: any) {
    let value = parameter.VALUE.code;
    Generator.addCode(`${value}`);
    } 
}
